package java.awt;

import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleRole;

import me.w70.bot.Bot;
import me.w70.bot.events.EventCaster;
import me.w70.bot.events.PaintEvent;

public class Canvas extends Component implements Accessible {

	public BufferedImage canvasImage = null;

	public Graphics canvasGraphics = null;

	public EventCaster eventCaster;

	public PaintEvent paintEvent = new PaintEvent();

	private Bot bot = null;

	public Canvas() {
		canvasImage = new BufferedImage(765, 503, BufferedImage.TYPE_INT_RGB);
		canvasGraphics = canvasImage.getGraphics();

	}

	public Canvas(GraphicsConfiguration graphicsconfiguration) {
		this();
		graphicsConfig = graphicsconfiguration;
	}

	public void setBot(Bot bot) {

		this.bot = bot;
		eventCaster = bot.getEventCaster();
	}

	public Graphics getGraphics() {
		Bot.currentScreen = canvasImage;
		try {
			if (Bot.getSelf() != null && bot == null) {

				setBot(Bot.getSelf());
				Bot.gui.rs.hackInput();
			}
			paintEvent.graphics = canvasGraphics;
			eventCaster.firePaint(paintEvent);

		} catch (Exception e) {
		}
		Thread.yield();
		getCanvasGraphics().drawImage(canvasImage, 0, 0, null);
		canvasGraphics = canvasImage.createGraphics();

		return (canvasGraphics);
	}

	public Graphics getCanvasGraphics() {
		return super.getGraphics();
	}

	public void setLocation(int x, int y) {
		super.setLocation(0, 0);
	}

	String constructComponentName() {
		return base + nameCounter++;
	}

	public void addNotify() {
		synchronized (getTreeLock()) {
			if (peer == null)
				peer = getToolkit().createCanvas(this);
			super.addNotify();
		}
	}

	public void paint(Graphics g) {
		paintEvent.graphics = g;
		eventCaster.firePaint(paintEvent);
	}

	public void update(Graphics g) {

		paintEvent.graphics = g;
		eventCaster.firePaint(paintEvent);
		// g.clearRect(0, 0, width, height);
		paint(g);
	}

	boolean postsOldMouseEvents() {
		return true;
	}

	public void createBufferStrategy(int i) {
		super.createBufferStrategy(i);
	}

	public void createBufferStrategy(int i,
			BufferCapabilities buffercapabilities) throws AWTException {
		super.createBufferStrategy(i, buffercapabilities);
	}

	public BufferStrategy getBufferStrategy() {
		return super.getBufferStrategy();
	}

	public AccessibleContext getAccessibleContext() {
		if (accessibleContext == null)
			accessibleContext = new AccessibleAWTCanvas();
		return accessibleContext;
	}

	private static final String base = "canvas";

	private static int nameCounter = 0;

	private static final long serialVersionUID = 0xe04a7a764a4bce12L;

	protected class AccessibleAWTCanvas extends
			Component.AccessibleAWTComponent {

		public AccessibleRole getAccessibleRole() {
			return AccessibleRole.CANVAS;
		}

		private static final long serialVersionUID = 0xa836fb5a1f61df35L;

		final Canvas this$0;

		protected AccessibleAWTCanvas() {
			super();
			this$0 = Canvas.this;
		}
	}

}